package com.baikeyang.app.controller.admin;


import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.model.User;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.user.IUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lenovo on 2018/7/23.
 */
@RestController(value = "userController")
@RequestMapping(value = "/admin/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "index")
    public ModelAndView index () {
        return new ModelAndView("/admin/user/user-index");
    }

    @ResponseBody
    @RequestMapping(value = "list")
    public PageResult getSysLogs (String page, String recPerPage, String search, String sortBy, String order) {
        //list?page=0&recPerPage=10&sortBy=&order=asc
        PageResult pagers = new PageResult();
        try{
            Integer size = 10;
            Integer current = 1;
            if(StringUtils.isNotEmpty(recPerPage)){
                try {
                    size = Integer.parseInt(recPerPage);
                } catch (Exception e) {}
            }
            if(StringUtils.isNotEmpty(page)) {
                try {
                    current = Integer.parseInt(page);
                    if(current < 1){current = 1;}
                } catch (Exception e) {}
            }
            Page<User> pageParam = new Page<>(current, size);
            //Page<User> pageParam = new Page<User>();
//            pageParam.setSize(size);
//            pageParam.setCurrent(current);
            IPage<User> pageResult = userService.getUsersPage(pageParam);
            pagers.setData(pageResult.getRecords());
            pagers.setPager(current, pageResult.getTotal());
            pagers.setResult("success");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return pagers;
    }

    @RequestMapping(value = "add")
    public ModelAndView add () {
        return new ModelAndView("/admin/user/user-add");
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit (String id) {
        return new ModelAndView("/admin/user/user-edit")
                .addObject("user", userService.getUserById(new Long(id)));
    }

    @RequestMapping(value = "show")
    public ModelAndView show () {
        return new ModelAndView("/admin/user/user-show");
    }

    @RequestMapping(value = "save")
    public ResponseResult save (HttpServletRequest request, User user) {
//        Long currentUserId = (Long) request.getSession().getAttribute("currentUserId");
//        Long currentUserId3 = (Long) SecurityUtils.getSubject().getSession().getAttribute("currentUserId");
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return userService.saveUser(user);
    }

    @RequestMapping(value = "update")
    public ResponseResult update (User user) {
        return userService.update(user);
    }

    @RequestMapping(value = "delete")
    public ResponseResult delete (String ids) {
        return userService.delete(ids);
    }

}
